package com.yuyou.fn.educational.mapper;

import com.github.wens.mybatisplus.mapper.AutoMapper;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.vo.SimpleQueryVo;
import com.yuyou.fn.educational.vo.BusTeamPeriodGradeClassNumVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 *
 * CourseClass 表数据库控制层接口
 *
 */
public interface CourseClassMapper extends AutoMapper<CourseClass,Long> {


    @Update("update course_class set room_id = null , room_name = null , is_arrange_room = 0,update_time =now() where class_id = #{classId}")
    void clearRoom(@Param("classId") Long classId);

    @Update("update course_class set start_course_time = null , end_course_time = null , class_date_tip = null , studying_time = null , is_arrange_time = 0 ,update_time =now() where class_id = #{classId}")
    void clearClassTime(@Param("classId")Long classId);

    @Select("select distinct subject_id  from  course_class  where campus_id = #{campusId} and period_id = #{periodId} ")
    @ResultType(Long.class)
    List<Long> howManySubjectForCampusAndPeriod(@Param("campusId")Long campusId,@Param("periodId") Long periodId);

    @Select("select distinct product_id from course_class where bus_team_id  = #{busTeamId} ")
    @ResultType(Long.class)
    List<Long> howManyProductForBusTeam(@Param("busTeamId") Long busTeamId);


    @Select("<script>\n" +
            "select distinct product_id from course_class where bus_team_id  = #{busTeamId} and  period_id = #{periodId}" +
            "<if test=\"statusList!=null and statusList.size()!=0\">\n"+
            "\tAND status in "+
            "<foreach item=\"status\" collection=\"statusList\" separator=\",\" open=\"(\" close=\")\" index=\"\">"+
            "#{status}"+
            "</foreach>"+
            "</if>\n"+
            "</script>"
            )
    @ResultType(Long.class)
    List<Long> howManyNowProductForBusTeam(@Param("busTeamId") Long busTeamId,@Param("statusList") List<Integer> statusList,@Param("periodId")Long periodId );



    @Select("select distinct CONCAT(CONCAT ( grade_id , '#'), subject_id)  from course_class where bus_team_id  = #{busTeamId}  ")
    @ResultType(Long.class)
    List<String> howManySubjectIdGradeIdForBusTeam(@Param("busTeamId")Long busTeamId);


    @Select("<script>\n" +
            "select distinct CONCAT(CONCAT ( grade_id , '#'), subject_id)  from course_class where bus_team_id  = #{busTeamId} and period_id = #{periodId} " +
            "<if test=\"statusList!=null and statusList.size()!=0\">\n"+
            "\tAND status in "+
            "<foreach item=\"status\" collection=\"statusList\" separator=\",\" open=\"(\" close=\")\" index=\"\">"+
            "#{status}"+
            "</foreach>"+
            "</if>\n"+
            "</script>"
        )
    @ResultType(Long.class)
    List<String> howManyNowSubjectIdGradeIdForBusTeam(@Param("busTeamId")Long busTeamId,@Param("statusList") List<Integer> statusList,@Param("periodId")Long periodId );

    @Update("update course_class set reg_amount = reg_amount + 1,degree_update_time=NOW() where class_id = #{classId} and ( reg_amount + lock_amount ) < quota  ")
    int increaseRegAmount(@Param("classId") Long classId);

    @Update("update course_class set reg_amount = reg_amount - 1 ,degree_update_time=NOW() where class_id = #{classId} and reg_amount > 0 ")
    int decreaseRegAmount(@Param("classId") Long classId);

    @Update("update course_class set progress_status = 2 , status = 3 ,update_time=NOW()  where completed_lecture_amount = lecture_amount and progress_status = 1 and status in (1,3) and DATE_ADD(end_course_time, INTERVAL 5 day ) < now()")
    void updateEndCourseStatus();

    void clearTeacher(@Param("classId")Long classId);

    @Update("update course_class cc set cc.reg_amount = ( select count( DISTINCT student_id ) from student_reg  where class_id  = #{classId}  and reg_status  = 0  ) where cc.class_id = #{classId} ")
    void mergeDegree(@Param("classId")Long classId);

    List<BusTeamPeriodGradeClassNumVo> findBusTeamPeriodGradeClassNum(@Param("simpleQueryVo")SimpleQueryVo simpleQueryVo, @Param("begin")int begin, @Param("pageSize") int pageSize);

    Integer countBusTeamPeriodGradeClassNum(@Param("simpleQueryVo")SimpleQueryVo simpleQueryVo);


    @Update("<script>\n" +
            "\tupdate course_class set lock_amount = lock_amount - 1,degree_update_time=NOW() where class_id in \n" +
            "\t<foreach collection=\"classIds\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
            "\t\t  #{id}\n" +
            "\t</foreach>\n" +
            "\t<![CDATA[ and lock_amount > 0\n" +
            "]]></script>")
    int decreaseLockAmount(@Param("classIds") List<Long> classIds);

    @Update("<script>\n" +
            "\tupdate course_class set lock_amount = lock_amount + 1 ,degree_update_time=NOW() where class_id in \n" +
            "\t<foreach collection=\"classIds\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
            "\t\t  #{id}\n" +
            "\t</foreach>\n" +
            "\t<![CDATA[ and ( reg_amount + lock_amount ) < quota\n" +
            "]]></script>")
    int increaseLockAmount(@Param("classIds") List<Long> classIds);
}